package com.jindy.module.oacopy.service.impl;

import com.jindy.module.oacopy.domain.SyOaCopy;
import com.jindy.module.oacopy.mapper.SyOaCopyMapper;
import com.jindy.module.oacopy.service.ISyOaCopyService;
import com.jindy.module.oacopyconfig.domain.SyOaCopyConfig;
import com.jindy.module.oacopyconfig.mapper.SyOaCopyConfigMapper;
import com.jindy.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 抄送Service业务层处理
 * 
 * @author jindy
 * @date 2023-05-12
 */
@Service
public class SyOaCopyServiceImpl implements ISyOaCopyService 
{
    @Resource
    private SyOaCopyMapper syOaCopyMapper;

    @Resource
    private SyOaCopyConfigMapper syOaCopyConfigMapper;

    /**
     * 查询抄送
     * 
     * @param id 抄送主键
     * @return 抄送
     */
    @Override
    public SyOaCopy selectSyOaCopyById(Long id)
    {
        return syOaCopyMapper.selectSyOaCopyById(id);
    }

    /**
     * 查询抄送列表
     * 
     * @param syOaCopy 抄送
     * @return 抄送
     */
    @Override
    public List<SyOaCopy> selectSyOaCopyList(SyOaCopy syOaCopy)
    {
        return syOaCopyMapper.selectSyOaCopyList(syOaCopy);
    }

    /**
     * 新增抄送
     * 
     * @param syOaCopy 抄送
     * @return 结果
     */
    @Override
    public int insertSyOaCopy(SyOaCopy syOaCopy)
    {
        syOaCopy.setCreateTime(DateUtils.getNowDate());
        return syOaCopyMapper.insertSyOaCopy(syOaCopy);
    }

    /**
     * 修改抄送
     * 
     * @param syOaCopy 抄送
     * @return 结果
     */
    @Override
    public int updateSyOaCopy(SyOaCopy syOaCopy)
    {
        syOaCopy.setUpdateTime(DateUtils.getNowDate());
        return syOaCopyMapper.updateSyOaCopy(syOaCopy);
    }

    /**
     * 批量删除抄送
     * 
     * @param ids 需要删除的抄送主键
     * @return 结果
     */
    @Override
    public int deleteSyOaCopyByIds(Long[] ids)
    {
        return syOaCopyMapper.deleteSyOaCopyByIds(ids);
    }

    /**
     * 删除抄送信息
     * 
     * @param id 抄送主键
     * @return 结果
     */
    @Override
    public int deleteSyOaCopyById(Long id)
    {
        return syOaCopyMapper.deleteSyOaCopyById(id);
    }

    /**
     * 批量新增抄送
     * @param oaCopyList 抄送
     * @return
     */
    @Override
    public int insertBathOaCopy(List<SyOaCopy> oaCopyList) {
        return syOaCopyMapper.insertBathOaCopy(oaCopyList);
    }

    /**
     * 添加抄送人
     * @param number 编号
     * @param orderId 订单id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addCopyUsers(String number, Long orderId) {
        SyOaCopyConfig syOaCopyConfig = new SyOaCopyConfig();
        syOaCopyConfig.setNumber(number);
        List<SyOaCopyConfig> syOaCopyConfigs = syOaCopyConfigMapper.selectSyOaCopyConfigList(syOaCopyConfig);
        if (!CollectionUtils.isEmpty(syOaCopyConfigs)){
            List<SyOaCopy> list = new ArrayList<>();
            for (SyOaCopyConfig oaCopyConfig : syOaCopyConfigs) {
                SyOaCopy syOaCopy = new SyOaCopy();
                syOaCopy.setOrderId(orderId);
                syOaCopy.setOrderType(oaCopyConfig.getOrderType());
                syOaCopy.setApplyUserId(oaCopyConfig.getCopyUserId());
                syOaCopy.setApplyUserName(oaCopyConfig.getCopyUserName());
                list.add(syOaCopy);
            }
            syOaCopyMapper.insertBathOaCopy(list);
        }
    }
}
